package com.twotiger.and.activity.others;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.AbastractListItem;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.BaseListItem;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.activity.user.ModifyLoginPwdPage;
import com.twotiger.and.adapter.AccountSettingAdapter;
import com.twotiger.and.adapter.BaseListAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.BroswerUrl;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.bean.User;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.view.ToggleButton;
import com.twotiger.and.view.ToggleButton.OnToggleChanged;

/**
 * 账户管理
 * @Description: 
 * @Author douxy
 * @Date 2015年8月13日 下午10:59:57
 */
public class AccountSetting extends BaseActivity {

    private static final int GETUSERINFOSSUCESS = 0;
    private static final int GETUSERINFOFAIL = 1;
    protected static final int RESETSUCCES = 6;
    protected static final int RESETFAIL = 7;

    /**标题*/
    private SubTitle subTitle;
    private BaseListAdapter baseListAdapter;

    private ListView listview;
    private HashMap<String, String> parms;
    private User user;
    private ToggleButton gesturePwdBtn;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.account_setting_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        listview = (ListView) view.findViewById(R.id.base_list);
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("我的账户信息");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
    }
    
    @Override
    protected void processLogic() {
        parms = getParm();
        user = application.getUser();
        ArrayList<AbastractListItem> datas = new ArrayList<AbastractListItem>();
        final BaseActivity mActivity = (BaseActivity) AccountSetting.this;
        datas.add(new BaseListItem("姓名", (user.name == null ? user.userName : user.name), true,
            new BaseListItem.OnClick() {

                @Override
                public void onClick() {
                    Intent intent = new Intent(mActivity, UserNamepage.class);
                    mActivity.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out,
                        false);
                }
            }));
        datas.add(new BaseListItem("账号", user.phone, false, null));
        datas.add(new BaseListItem("托管账号", user.thirdAccountName, false, null));

        datas.add(new BaseListItem("登录密码", "点击可重置", true, new BaseListItem.OnClick() {
            @Override
            public void onClick() {
                Intent intent = new Intent(mActivity, ModifyLoginPwdPage.class);
                mActivity
                    .startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            }
        }));
        datas.add(new BaseListItem("托管账号密码", "点击可重置", false, new BaseListItem.OnClick() {
            @Override
            public void onClick() {

                ConstantDataUnit zhmmgl = getConstantDataByKey(Constants.dz_zhmmgl);
                if(zhmmgl != null){
                    Intent intent = new Intent(mContext, WebViewPage.class);
                    intent.putExtra("title", zhmmgl.getName());
                    intent.putExtra("url", zhmmgl.getVal() + "?token=" + getToken());
                    intent.putExtra("BACK_MODE", WebViewPage.BACK_DIRECT);
                    startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                }

            }
        }));
        BaseListItem shoushi = new BaseListItem("手势密码", null, false, null);
        shoushi.toggleChangedIsOn = application.otherService.gestureIsOpened();
        shoushi.onToggleChanged = new OnToggleChanged() {

            @Override
            public void onToggle(boolean on) {
                if (!on) {//关闭手势提醒确认
                    if (gesturePwdBtn == null) {
                        gesturePwdBtn = (ToggleButton) listview.getChildAt(5).findViewById(
                            R.id.base_list_item_toggleBtn);
                    }
                    PromptManager.showConfirmAlertCommon(AccountSetting.this, 0,
                        "温馨提示", "确认关闭手势密码？\n(关闭后，每次都需要输入登录密码)", "取消", "确认", true,
                        new ReChargeClickListener() {

                            @Override
                            public void onClickConfirm() {
                                gesturePwdBtn.setToggleOn();
                            }

                            @Override
                            public void onClickCancel() {
                                application.closeGesture();
                            }
                        });

                } else {
                    if (gesturePwdBtn == null) {
                        gesturePwdBtn = (ToggleButton) listview.getChildAt(5).findViewById(
                            R.id.base_list_item_toggleBtn);
                    }
                    if (gesturePwdBtn != null) {
                        gesturePwdBtn.setToggleOn();
                    }
                    Intent intent = new Intent(mContext, EnsurePassword.class);
                    startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                }
            }
        };
        datas.add(shoushi);
        datas.add(new BaseListItem("设置手势密码", null, false, new ItemClick(EnsurePassword.class)));

        baseListAdapter = new BaseListAdapter(this, datas);
        listview.setAdapter(baseListAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (gesturePwdBtn != null) {
            if (application.otherService.gestureIsOpened()) {
                gesturePwdBtn.setToggleOn();
            } else {
                gesturePwdBtn.setToggleOff();
            }
        } else {
            if (listview.getChildAt(5) != null) {
                gesturePwdBtn = (ToggleButton) listview.getChildAt(5).findViewById(
                    R.id.base_list_item_toggleBtn);
                if (gesturePwdBtn != null) {
                    if (application.otherService.gestureIsOpened()) {
                        gesturePwdBtn.setToggleOn();
                    } else {
                        gesturePwdBtn.setToggleOff();
                    }
                }
            }
        }
        
        if(!isLogin()){
           finish(); 
        }
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                user = JSON.parseObject((String) msg.obj, User.class);
                switch (msg.what) {
                case GETUSERINFOSSUCESS:
                    if (user.isOk()) {
                        listview.setAdapter(new AccountSettingAdapter(AccountSetting.this, user,
                            handler));
                    }
                    break;
                case GETUSERINFOFAIL:

                    break;

                case RESETSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        BroswerUrl url = JSON.parseObject(result.data, BroswerUrl.class);
                        Intent intent = new Intent(mContext, WebViewPage.class);
                        intent.putExtra("title", "修改汇付密码");
                        intent.putExtra("url", url.getUrl());
                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                    }
                    break;
                case RESETFAIL:

                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        default:
            break;
        }
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    class ItemClick implements BaseListItem.OnClick {
        private Class<? extends Activity> clazz;

        public ItemClick(Class<? extends Activity> clazz) {
            this.clazz = clazz;
        }

        @Override
        public void onClick() {
            Intent intent = new Intent(mContext, clazz);
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
        }

    }
}
